/**
 * 根据身份证号码计算出生年月
 * @param idCard
 */
export function getBirthdayByIdCard(idCard) {
    if (idCard.length === 15) {
        return ['19' + idCard.substr(6, 2), idCard.substr(8, 2), idCard.substr(10, 2)].join('-')
    } else if (idCard.length === 18) {
        return [idCard.substr(6, 4), idCard.substr(10, 2), idCard.substr(12, 2)].join('-')
    }

    return ''
}

/**
 * 根据身份证号码计算性别
 * @param idCard
 */
export function getSexByIdCard(idCard) {
    if (idCard.length === 15) {
        return ['2', '1'][idCard.substr(14, 1) % 2]
    } else if (idCard.length === 18) {
        return ['2', '1'][idCard.substr(16, 1) % 2]
    }

    return ''
}

/**
 * 根据身份证号码计算年龄
 * @param idCard
 */
export function getAgeByIdCard(idCard) {
    //获取用户身份证号码
    const userCard = idCard

    //如果用户身份证号码为undefined则返回空
    if (!userCard) {
        return 0
    }

    // 获取出生日期
    const yearBirth = userCard.substring(6, 10)
    const monthBirth = userCard.substring(10, 12)
    const dayBirth = userCard.substring(12, 14)
    // 获取当前年月日并计算年龄
    const myDate = new Date()
    const monthNow = myDate.getMonth() + 1
    const dayNow = myDate.getDate()
    let age = myDate.getFullYear() - yearBirth

    if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
        age--
    }

    // 得到年龄
    return age
}